package com.hbun.cxcy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hbun.cxcy.entity.File;
import com.hbun.cxcy.entity.Swiper;
import com.hbun.cxcy.mapper.SwiperMapper;
import com.hbun.cxcy.service.FileService;
import com.hbun.cxcy.service.SwiperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbun.cxcy.vo.FileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mahaunyu
 * @since 2022-01-27
 */
@Service
public class SwiperServiceImpl extends ServiceImpl<SwiperMapper, Swiper> implements SwiperService {

    @Autowired
    FileService fileService;
    /**
     * 将数据保存到swpier和file两个表中
     * @param fileVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveWithCustom(FileVo fileVo) {
        Swiper swiper = new Swiper();
        swiper.setSwiperUrl(fileVo.getFilePath());
        this.save(swiper);
        String name = fileVo.getName();
        // 文件后缀名
        String suffix = name.split("\\.")[1];
        File file = new File();
        file.setName(name);
        file.setFilePath(fileVo.getFilePath());
        file.setSuffix(suffix);
        file.setCreateTime(new Date());
        file.setUpdateTime(new Date());
        file.setStatus(1);
        file.setType("swiper");
        fileService.save(file);

    }

    /**
     * 逻辑删除
     * @param ids
     */
    @Override
    public void removeByIdsWithCustom(List<Long> ids) {
        BaseMapper<File> baseMapper = fileService.getBaseMapper();
        for (int i = 0; i < ids.size(); i++) {

            File file = baseMapper.selectById(ids.get(i));
            file.setStatus(0);
            baseMapper.updateById(file);
        }
    }

    @Override
    public List<File> listByCustom() {
        QueryWrapper<File> wrapper = new QueryWrapper<File>().eq("type","swiper");
        wrapper.and(w->{
            w.eq("status",1);
        });
        List<File> list = fileService.list(wrapper);
        return list;
    }
}
